package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCardMapper;
import com.sky.service.ShoppingCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCardServiceImpl implements ShoppingCardService {
    @Autowired
    private ShoppingCardMapper shoppingCardMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /*
    * 添加购物车*/
    public void addShoppingCard(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 获取userId
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 查询商品获取商品的返回值
        List<ShoppingCart> list = shoppingCardMapper.list(shoppingCart);

        // 判断加入的商品是否存在
        if(list != null && list.size()>0){
            ShoppingCart cart = list.get(0);
            // 如果存在,只需要数量+1
            cart.setNumber(cart.getNumber()+1);
            shoppingCardMapper.updateNumber(cart);
        }else{
            // 如果不存在,则直接添加
            //判断是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();

            if(dishId != null){
                // 本次添加购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                // 本次添加购物车的是套餐
                Setmeal setmeal = setmealMapper.getId(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            // 设置两者都需添加的信息
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 将数据添加到数据库中
            shoppingCardMapper.insert(shoppingCart);
        }


    }

    /*展示购物车*/
    public List<ShoppingCart> showShppiingCard() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCardMapper.list(shoppingCart);
        return list;
    }

    /*清空购物车*/
    public void cleanShoppingcard() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCardMapper.clean(shoppingCart);
    }

    /*删除购物车中的一个商品*/
    public void deleteShoppingCard(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        // 获取商品信息
        List<ShoppingCart> lists = shoppingCardMapper.list(shoppingCart);

        if (lists.size() > 0 && lists != null) {
            // 判断商品是一个还是一个以上
            shoppingCart = lists.get(0);

            if (shoppingCart.getNumber() > 1){
                // 商品是一个以上，删除一条
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCardMapper.updateNumber(shoppingCart);
            }else{
                // 商品是一个，直接从数据库中删除
                shoppingCardMapper.delete(shoppingCart);
            }
        }
    }
}
